<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: Ringo Chan <334258471@qq.com>
// +----------------------------------------------------------------------

namespace Admin\Controller;

/**
 * 后台订单控制器
 * 
 * @author Ringo Chan<334258471@qq.com>
 */
class CheckupController extends AdminController{
    
    /**
     * 商品订单列表
     * 
     * @author Ringo Chan<334258471@qq.com>
     */
    public function index(){
        $username = I('username');
        $status = I('status');
        
        $map['status']  =   array('egt', 0);
        
        if ($username){
            $M_ucenter_member = M('ucenter_member');
            $members = $M_ucenter_member->where(array('id|username'=>array(intval($username),array('like','%'.$username.'%'),'_multi'=>true)))->select();
            
            $ids = array();
            foreach ($members as $member){
                $ids[] = $member['id'];
            }
            $map['uid'] = array('in', $ids);
        }
            
        $status && $map['status'] = $status;
        
        $list   = $this->lists('Checkup', $map);
        int_to_string($list, array('status'=>getStatus('checkup_status')));
        
        $this->assign('_list', $list);
        $this->meta_title = '商品订单列表';
        $this->display();
    }
    
    /**
     * 查看/编辑 商品订单
     * 
     * @param integer id 订单编号
     * @author Ringo Chan<334258471@qq.com>
     */
    public function edit(){
        if(IS_POST){
            $id = I('get.id');
            
            $M_checkup = M('Checkup');
            
            $data=array();
            $total=I('post.total');
            $shipping_price=I('post.shipping_price');
            $identify_price=I('post.identify_price');
            $status=I('post.status');
            $remark=I('post.remark');
            
            !(is_numeric($total) && $total > 0) && $this->error('总价错误');
            !(is_numeric($identify_price) && $identify_price > 0) && $this->error('鉴定费用错误');
            !(is_numeric($shipping_price) && $shipping_price > 0) && $this->error('运费错误');
            
            $data['total']=$total;
            $data['shipping_price']=$shipping_price;
            $data['identify_price']=$identify_price;
            $data['status']=$status;
            $data['remark']=$remark;
            $data['modify_time']=NOW_TIME;
            if((int)$status == 3){
                $data['identify_price']=NOW_TIME;
            }
            $map=array();
            $map['id']=$id;
            
            $result=$M_checkup->where($map)->save($data);
            
            $result ===false  && $this->error('保存出错');
            
            $this->success('成功',U('index'));
        }else{
            
        $id = I('get.id');
        
        $M_checkup = M('Checkup');
        $M_checkup_shipping = M('checkup_shipping');
        
        $info = $M_checkup->find($id);
        !$info && $this->error('非法的参数编号');
        
        $seller_shipping = $M_checkup_shipping->where(array('checkup_id'=>$id, 'type'=>'seller'))->find();
        $admin_shipping = $M_checkup_shipping->where(array('checkup_id'=>$id, 'type'=>'admin'))->find();
        $seller_shipping['shipping'] = getExpress($seller_shipping['shipping_code'], $seller_shipping['code']);
        $admin_shipping['shipping'] = getExpress($admin_shipping['shipping_code'], $admin_shipping['code']);
        
      
        $this->assign('info', $info);
        $this->assign('seller_shipping', $seller_shipping);
        $this->assign('admin_shipping', $admin_shipping);
        $this->meta_title = '鉴定详情';
        $this->display();
        
        }
    }
    
    /**
     * 发往买家
     * 
     * @param integer id 订单编号
     * @author Ringo Chan<334258471@qq.com>
     */
    public function shipping($id = 0 , $code = '', $shipping_code = ''){
       
        $M_shipping = M('shipping');
        $M_checkup = M('Checkup');
        $M_checkup_shipping = M('checkup_shipping');
        
        $info = $M_checkup->find($id);
        !$info && $this->error('非法的参数编号');
        
        if (IS_POST) {
            empty($shipping_code) && $this->error('请选择物流公司');
            $shipping_info = $M_shipping->where(array('code'=>$shipping_code))->find();
            !$shipping_info && $this->error('请选择物流公司');
            empty($code) && $this->error('快递单号不能为空');
            
            $order_shipping_id = $M_checkup_shipping->add(array('type'=>'admin', 'checkup_id'=>$id, 'code'=>$code, 'shipping_code'=>$shipping_code, 'create_time'=>NOW_TIME));
            
            if ($order_shipping_id > 0){
                $M_checkup->where(array('id'=>$id))->setField('status', 4);
                $this->success('发往买家成功', U('Checkup/index'));
            } else {
                $this->error('发往失败，未知错误');
            }
        } else {
            $this->assign('info', $info);
            $this->meta_title = '发往买家';
            $this->display();
        } 
    }
    
    /**
     * 充值订单列表
     */
    public function payment(){
        $username = I('username');
        $status = I('status');
        
        $map['payment_code'] = array('neq', '');
        $map['method'] = '+';
        $map['status'] = array('egt', 0);
        
        if ($username){
            $M_ucenter_member = M('ucenter_member');
            $members = $M_ucenter_member->where(array('id|username'=>array(intval($username),array('like','%'.$username.'%'),'_multi'=>true)))->select();
        
            $ids = array();
            foreach ($members as $member){
                $ids[] = $member['id'];
            }
            $map['uid'] = array('in', $ids);
        }
        
        $status && $map['status'] = $status;
        
        $list   = $this->lists('payment_log', $map, 'create_time DESC');
        int_to_string($list, array('status'=>getStatus('payment')));
        
        $this->assign('_list', $list);
        $this->meta_title = '充值订单列表';
        $this->display();
    }
    
    /**
     * 通用改变状态
     *
     * @param string $method 方法名
     * @param number $status 状态值
     * @param array id 编号
     * @author Ringo Chan <334258471@qq.com>
     */
    public function changeStatus($method=null, $status=0){
        $map['id'] =   array('in',I('id', 0));
        
        $data['status'] = $status;
        $data['update_time'] = NOW_TIME;
        
        $feedback = false;
    
        switch ( strtolower($method) ){
            case 'checkup':
                $M_checkup = M('checkup');
                $feedback = $M_checkup->where($map)->save($data);
                break;
            default:
                $this->error('参数非法');
        }
    
        $feedback !== false ? $this->success('状态修改成功！') : $this->error('状态修改失败！');
    }
}
?>